<template>
  <div class="login-container">
    <div class="login-box">
      <h2 class="login-title">摄影作品管理系统</h2>
      
      <el-tabs v-model="activeTab" type="card" class="login-tabs">
        <el-tab-pane label="参赛者登录" name="contestant">
          <el-form :model="loginForm" :rules="loginRules" ref="loginForm" label-width="0px">
            <el-form-item prop="username">
              <el-input v-model="loginForm.username" prefix-icon="el-icon-user" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="loginForm.password" prefix-icon="el-icon-lock" type="password" placeholder="密码" @keyup.enter.native="submitForm"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :loading="loading" @click="submitForm" class="login-btn">登录</el-button>
            </el-form-item>
            <div class="login-links">
              <span>还没有账号？</span>
              <router-link to="/register">立即注册</router-link>
              <span class="separator">|</span>
              <router-link to="/contest">直接参赛报名</router-link>
            </div>
          </el-form>
        </el-tab-pane>
        
        <el-tab-pane label="管理员登录" name="admin">
          <el-form :model="adminForm" :rules="loginRules" ref="adminForm" label-width="0px">
            <el-form-item prop="username">
              <el-input v-model="adminForm.username" prefix-icon="el-icon-user" placeholder="管理员账号"></el-input>
            </el-form-item>
            <el-form-item prop="password">
              <el-input v-model="adminForm.password" prefix-icon="el-icon-lock" type="password" placeholder="管理员密码" @keyup.enter.native="submitAdminForm"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" :loading="adminLoading" @click="submitAdminForm" class="login-btn">管理员登录</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      
      <div class="login-notice">
        <p v-if="activeTab === 'contestant'">参赛者登录后可以管理您的参赛作品和查看参赛状态</p>
        <p v-else>管理员登录用于系统管理，普通用户请使用参赛者登录</p>
      </div>
    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'

export default {
  name: 'Login',
  data() {
    return {
      activeTab: 'contestant',
      loginForm: {
        username: '',
        password: ''
      },
      adminForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, message: '密码长度不少于6位', trigger: 'blur' }
        ]
      },
      loading: false,
      adminLoading: false
    }
  },
  methods: {
    ...mapActions(['login']),
    submitForm() {
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          try {
            // 创建包含type参数的登录数据
            const loginData = {
              ...this.loginForm,
              type: 3
            };
            console.log('Contestant login request data:', loginData);
            const res = await this.$http.post('/users/login', loginData);
            if (res.data.code === 1) {
              // 参赛者登录成功
              console.log('Contestant login successful, user data:', res.data.data);
              this.login({
                token: res.data.data.id, // 使用用户ID作为token
                userInfo: res.data.data
              })
              this.$message.success('登录成功')
              // 跳转到用户中心
              this.$router.push('/user/profile')
            } else {
              this.$message.error(res.data.msg || '登录失败')
            }
          } catch (error) {
            console.error('登录请求出错', error)
            this.$message.error('登录失败，请稍后重试')
          } finally {
            this.loading = false
          }
        }
      })
    },
    submitAdminForm() {
      this.$refs.adminForm.validate(async valid => {
        if (valid) {
          this.adminLoading = true
          try {
            // 创建包含type参数的登录数据
            const adminLoginData = {
              ...this.adminForm,
              type: 1
            };
            console.log('Admin login request data:', adminLoginData);
            const res = await this.$http.post('/users/login', adminLoginData);
            if (res.data.code === 1) {
              // 管理员登录成功
              console.log('Admin login successful, user data:', res.data.data);
              
              // Store user info in Vuex
              this.login({
                token: res.data.data.id,
                userInfo: res.data.data
              })
              
              this.$message.success('管理员登录成功')
              // 跳转到管理员页面
              this.$router.push('/admin/photos')
            } else {
              this.$message.error(res.data.msg || '登录失败')
            }
          } catch (error) {
            console.error('登录请求出错', error)
            this.$message.error('登录失败，请稍后重试')
          } finally {
            this.adminLoading = false
          }
        }
      })
    }
  },
  created() {
    // 从URL参数获取默认标签页
    const tab = this.$route.query.tab
    if (tab === 'admin') {
      this.activeTab = 'admin'
    }
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  background-color: #f5f5f5;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-box {
  width: 400px;
  padding: 30px;
  background-color: #fff;
  border-radius: 5px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.login-title {
  text-align: center;
  margin-bottom: 20px;
  color: #409EFF;
}

.login-tabs {
  margin-bottom: 20px;
}

.login-btn {
  width: 100%;
}

.login-links {
  text-align: right;
  margin-top: 10px;
  font-size: 14px;
}

.login-links a {
  color: #409EFF;
  text-decoration: none;
}

.separator {
  margin: 0 5px;
  color: #dcdfe6;
}

.login-notice {
  margin-top: 20px;
  padding: 10px;
  background-color: #f8f8f8;
  border-radius: 4px;
  color: #909399;
  font-size: 12px;
  text-align: center;
}

/* 重写element-ui的tabs样式 */
:deep(.el-tabs__header) {
  margin-bottom: 20px;
}

:deep(.el-tabs__item) {
  font-size: 16px;
  height: 40px;
  line-height: 40px;
}
</style> 